//
//  YWSearchAllViewController.m
//  聆听FM
//
//  Created by dllo on 16/6/18.
//  Copyright © 2016年 Dark. All rights reserved.
//

#import "YWSearchAllViewController.h"
#import "YWAnchorVoiceModel.h"
#import "YWFansModel.h"
#import "YWSearchOutModel.h"
#import "ZCSTipsReTableViewCell.h"
#import "YWAnchorVoiceTableViewCell.h"
#import "YWFansTableViewCell.h"
#import "AlbumDetailViewController.h"
#import "ZCSPlayMusicViewController.h"
#import "YWAnchorIntroduceViewController.h"

typedef void(^searchBlock)(NSMutableArray *array);

@interface YWSearchAllViewController ()<UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) UITableView *allTableView;

@property (nonatomic, strong) NSMutableArray *albumArray;

@property (nonatomic, strong) NSMutableArray *trackArray;

@property (nonatomic, strong) NSMutableArray *userArray;

@property (nonatomic, retain) YWSearchOutModel *outModel;

@property (nonatomic, retain) NSMutableArray *playArray;
@property (nonatomic, copy) searchBlock aBlock;

@end

@implementation YWSearchAllViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self getView];
    [self getData];
}

- (void)getView
{
    self.allTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, SCREENWIDTH, SCREENHEIGHT - 64 - 35) style:UITableViewStyleGrouped];
    _allTableView.delegate = self;
    _allTableView.dataSource = self;
    _allTableView.separatorStyle = 0;
    [self.view addSubview:_allTableView];
    
    [_allTableView registerNib:[UINib nibWithNibName:@"ZCSTipsReTableViewCell" bundle:nil] forCellReuseIdentifier:@"TipCell"];
    [_allTableView registerClass:[YWAnchorVoiceTableViewCell class] forCellReuseIdentifier:@"voiceCell"];
    [_allTableView registerClass:[YWFansTableViewCell class] forCellReuseIdentifier:@"vipCell"];
    [_allTableView registerClass:[YWFansTableViewCell class] forCellReuseIdentifier:@"faultVipCell"];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 3;
}



- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    switch (section) {
        case 0:
            return _outModel.albumDocsModel.docs.count;
            break;
        case 1:
            return _outModel.trackDocsModel.docs.count;
            break;
        default:
            return _outModel.userDocsModel.docs.count;
            break;
    }
}



- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    switch (indexPath.section) {
        case 0:
            return 80;
            break;
        case 1:
        {
            YWSearchModel *searchModel = [_outModel.trackDocsModel.docs objectAtIndex:indexPath.row];
            if ([searchModel.title length] > 17) {
                return 100 * FITHEIGHT;
            }
            else
            {
                return 80 * FITHEIGHT;
            }
        }
            break;
        default:
        {
            if ([[_outModel.userDocsModel.docs objectAtIndex:indexPath.row] isVerified] == YES)
            {
                return 90 * FITHEIGHT;
            }
            else
            {
                return 60 * FITHEIGHT;
            }
        }
            break;
    }
}

- (void)getMoreDataWith:(NSInteger)albumId page:(NSInteger)page
{
    NSString *urlString = [NSString stringWithFormat:@"http://mobile.ximalaya.com/mobile/v1/album/track?albumId=%ld&device=iPhone&isAsc=true&pageId=%ld&pageSize=20", albumId, page];
    __weak YWSearchAllViewController *pmvc = self;
    [LTNetTool GetNetWithURL:urlString Body:nil Header:nil Response:0 Success:^(id result) {
        pmvc.playArray = [NSMutableArray array];
        for (NSDictionary *dic in result[@"data"][@"list"]) {
            ZCSListenListListModel *model = [[ZCSListenListListModel alloc] initWithDic:dic];
            [pmvc.playArray addObject:model];
        }
        pmvc.aBlock(_playArray);
        
    } Failure:^(NSError *error) {
        NSLog(@"%@", error);
    }];
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    switch (indexPath.section) {
        case 0:
        {
            YWSearchModel *albumModel = [self.outModel.albumDocsModel.docs objectAtIndex:indexPath.row];
            AlbumDetailViewController *album = [[AlbumDetailViewController alloc] init];
            album.albmId = albumModel.id;
            [self.navigationController pushViewController:album animated:YES];
        }
            break;
        case 1:
        {
            YWSearchModel *trackModel = [self.outModel.trackDocsModel.docs objectAtIndex:indexPath.row];
            [self getMoreDataWith:trackModel.album_id page:1];
            ZCSPlayMusicViewController *play = [ZCSPlayMusicViewController sharePlayVC];
            play.musicIndex = indexPath.row;
            __weak YWSearchAllViewController *sa = self;
            self.aBlock = ^(NSMutableArray *array){
                play.dataArray = array;
                play.albumId = trackModel.album_id;
                [sa presentViewController:play animated:YES completion:nil];
            };
            
            
        }
            break;
        case 2:
        {
            YWAnchorIntroduceViewController *invc = [[YWAnchorIntroduceViewController alloc] init];
            invc.uid = [[self.outModel.userDocsModel.docs objectAtIndex:indexPath.row] uid];
            invc.hidesBottomBarWhenPushed = YES;
            [self.navigationController pushViewController:invc animated:YES];
            
        }
            break;
        
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        ZCSTipsReTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"TipCell"];
        cell.searchModel = [_outModel.albumDocsModel.docs objectAtIndex:indexPath.row];
        return cell;
    }
    else if (indexPath.section == 1)
    {
        YWAnchorVoiceTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"voiceCell"];
        cell.searchModel = [_outModel.trackDocsModel.docs objectAtIndex:indexPath.row];
        return cell;
    }
    else
    {
        if ([[_outModel.userDocsModel.docs objectAtIndex:indexPath.row] isVerified] == YES) {
            YWFansTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"vipCell"];
            cell.searchModel = [_outModel.userDocsModel.docs objectAtIndex:indexPath.row];
            return cell;
        }
        else
        {
            YWFansTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"faultVipCell"];
            cell.searchModel = [_outModel.userDocsModel.docs objectAtIndex:indexPath.row];
            return cell;
        }

    }
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView *headView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREENWIDTH, 40 * FITHEIGHT)];
    headView.backgroundColor = [UIColor whiteColor];
    
    UIImageView *leftImageView = [[UIImageView alloc] initWithFrame:CGRectMake(15 * FITWIDTH, 15 * FITHEIGHT, 10 * FITHEIGHT, 10 * FITHEIGHT)];
    leftImageView.image = [UIImage imageNamed:@"findsection_logo"];
    [headView addSubview:leftImageView];
    
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(leftImageView.frame.origin.x + leftImageView.bounds.size.width + 5 * FITWIDTH, leftImageView.frame.origin.y, 50 * FITWIDTH, 10 * FITHEIGHT)];
    titleLabel.font = [UIFont systemFontOfSize:13];
    switch (section) {
        case 0:
            titleLabel.text = @"专辑";
            break;
        case 1:
            titleLabel.text = @"声音";
            break;
        case 2:
            titleLabel.text = @"主播";
            break;
        default:
            break;
    }
    [headView addSubview:titleLabel];
    
    UILabel *numLabel = [[UILabel alloc] initWithFrame:CGRectMake(SCREENWIDTH / 2, 15 * FITHEIGHT, SCREENWIDTH / 2 - 20 * FITWIDTH, 10 * FITHEIGHT)];
    numLabel.userInteractionEnabled = YES;
    numLabel.tag = 1000 + section;
    numLabel.textAlignment = 2;
    numLabel.font = [UIFont systemFontOfSize:13];
    switch (section) {
        case 0:
        {
            NSInteger numLength = [[NSString stringWithFormat:@"%ld", _outModel.albumDocsModel.numFound] length];
            NSString *string = [NSString stringWithFormat:@"全部%ld张专辑", _outModel.albumDocsModel.numFound];
            NSMutableAttributedString *str = [[NSMutableAttributedString alloc] initWithString:string];
            [str addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(2, numLength)];
            numLabel.attributedText = str;
        }
            break;
        case 1:
        {
            NSInteger numLength = [[NSString stringWithFormat:@"%ld", _outModel.trackDocsModel.numFound] length];
            NSString *string = [NSString stringWithFormat:@"全部%ld张声音", _outModel.trackDocsModel.numFound];
            NSMutableAttributedString *str = [[NSMutableAttributedString alloc] initWithString:string];
            [str addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(2, numLength)];
            numLabel.attributedText = str;
        }
            break;
        case 2:
        {
            NSInteger numLength = [[NSString stringWithFormat:@"%ld", _outModel.userDocsModel.numFound] length];
            NSString *string = [NSString stringWithFormat:@"全部%ld张用户", _outModel.userDocsModel.numFound];
            NSMutableAttributedString *str = [[NSMutableAttributedString alloc] initWithString:string];
            [str addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(2, numLength)];
            numLabel.attributedText = str;
        }
            break;
        default:
            break;
    }
    [headView addSubview:numLabel];
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(didTapNumLabel:)];
    [numLabel addGestureRecognizer:tap];
    
    UIImageView *arrowImageView = [[UIImageView alloc] initWithFrame:CGRectMake(SCREENWIDTH - 15, 15 * FITHEIGHT, 10 * FITWIDTH, 10 * FITWIDTH)];
    arrowImageView.image = [UIImage imageNamed:@"findcell_arrow"];
    [headView addSubview:arrowImageView];
    
    return headView;
}

- (void)didTapNumLabel:(UITapGestureRecognizer *)tap
{
    NSInteger num = tap.view.tag - 1000 + 1;
    _valueBlock(num);
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 40 * FITHEIGHT;
}

- (void)getData
{
    [self getMessageData];
}

- (void)getMessageData
{
    NSString *string = [NSString stringWithFormat:@"http://search.ximalaya.com/front/v1?core=all&device=iPhone&kw=%@&live=true&page=1&paidFilter=false&rows=3&spellchecker=true", _keyString];
    __weak YWSearchAllViewController *avc = self;
    [LTNetTool GetNetWithURL:string Body:nil Header:nil Response:LTJSON Success:^(id result) {
        avc.outModel = [[YWSearchOutModel alloc] initWithDic:result];
        if (avc.outModel.albumDocsModel.numFound == 0 && avc.outModel.trackDocsModel.numFound == 0 && avc.outModel.userDocsModel.numFound == 0) {
            NSLog(@"1");
            _imageBlock();
        }
        [_allTableView reloadData];
    } Failure:^(NSError *error) {
        NSLog(@"请求失败");
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
